//
//  WBTitleButton.swift
//  传智微博
//
//  Created by zhoujiming on 2017/2/10.
//  Copyright © 2017年 gmcc. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {

    // 如果title为空，则显示“首页”，否则，显示箭头和昵称
    init(title: String?) {
        super.init(frame: CGRect())
        
        if title == nil{
            setTitle("首页", for: [])
        }
        else
        {
            setTitle(title! + " ", for: [])
            
            
            setImage(UIImage(named:"navigationbar_arrow_down"), for: [])
            setImage(UIImage(named:"navigationbar_arrow_up"), for: [.selected])
        }
        
        // 设置字体
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel,
            let imageView = imageView else {
                return
        }
        
        // 将label的x向左移动imageview的宽度
        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
        
        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
    }
}
